package ${package.Controller};

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.MapBuilder;
import ${package.Entity}.${table.entityName};
import ${cfg.parent}.${package.ModuleName}.dto.${table.entityName}DTO;
import ${cfg.parent}.${package.ModuleName}.wrapper.${table.entityName}Wrapper;
import ${cfg.parent}.${package.ModuleName}.controller.form.${table.entityName}QueryForm;
<#--<#if cfg.entity??>
    <#list cfg.entity.joinTables as joinTable>
import ${cfg.parent}.${joinTable.targetEntity.moduleName}.entity.${joinTable.targetEntity.name};
    </#list>
</#if>-->
import ${package.Service}.${table.serviceName};
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * ${table.comment}控制器
 *
 * @@tableName ${table.name}
 * @author ${author}
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/${table.name?replace('_','/')}")
@Api("${table.comment}控制器")
public class ${table.entityName}Controller {

    private final ${table.serviceName} ${table.entityName?uncap_first}Service;

    @GetMapping("/detail")
    @ApiOperation(value = "通过主键id查询 ${table.comment}")
    public Result<?> query(@ApiParam("主键id") @NotNull(message = "必须提供需要查询的数据") Long id){
        return Result.ok(${table.entityName?uncap_first}Service.getById(id));
    }

    @PutMapping("/update")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id编辑 ${table.comment}")
    public Result<?> edit(@ApiParam("编辑表单") @RequestBody @Validated ${table.entityName}DTO dto){
        Assert.notNull(dto.getId(),"必须提供id");
        <#list table.fields as field>
        Assert.notNull(dto.get${field.propertyName?cap_first}(),"必须提供${field.comment}");
        </#list>
        ${table.entityName} ${table.entityName?uncap_first} = ${table.entityName?uncap_first}Service.requireGetById(dto.getId(),"相关数据不存在");
        <#list table.fields as field>
        ${table.entityName?uncap_first}.set${field.propertyName?cap_first}(dto.get${field.propertyName?cap_first}());
        </#list>
        return Result.status(${table.entityName?uncap_first}Service.updateById(${table.entityName?uncap_first}));
    }

    @PostMapping("/save")
    @RequiresAuthentication
    @ApiOperation(value = "新增 ${table.comment}")
    public Result<?> save(@ApiParam("新增表单") @RequestBody @Validated ${table.entityName}DTO dto){
        <#list table.fields as field>
        Assert.notNull(dto.get${field.propertyName?cap_first}(),"必须提供${field.comment}");
        </#list>
        ${table.entityName} ${table.entityName?uncap_first} = new ${table.entityName}();
        <#list table.fields as field>
        ${table.entityName?uncap_first}.set${field.propertyName?cap_first}(dto.get${field.propertyName?cap_first}());
        </#list>
        return Result.status(${table.entityName?uncap_first}Service.save(${table.entityName?uncap_first}));
    }

    @DeleteMapping("/remove")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id删除 ${table.comment}")
    public Result<?> remove(@ApiParam("主键id") @NotNull(message = "必须提供需要删除的数据") Long id){
        return Result.status(${table.entityName?uncap_first}Service.requireRemoveById(id, "抱歉，您的账号权限不支持删除此数据"));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<?> page(@ApiParam("查询表单") ${table.entityName}QueryForm queryForm){
        IPage<${table.entityName}Wrapper> page = ${table.entityName?uncap_first}Service.selectJoinListPage(queryForm.buildPageQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",page.getRecords())
                    .put("rows",page.getTotal())
                .build());
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<?> list(@ApiParam("查询表单") ${table.entityName}QueryForm queryForm){
        List<${table.entityName}Wrapper> list = ${table.entityName?uncap_first}Service.selectJoinList(queryForm.buildListQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",list)
                .build());
    }
}